#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "serverfunc.h"



void *acceptpthread(void *arg)
{
    printf("new thread!\n");
	int acceptfd=*(int *)arg;
	//char recvbuf[256]={0};
	tcpsock recvbuf;
    tcpsock sendbuf;
	int loginnum=-1;
	while(1)
	{
		if(0!=recv(acceptfd,&recvbuf,sizeof(tcpsock),0))
		{
            printf("next!!!\n");
			int codenum=recvbuf.code;
            printf("codenum = %d\n",codenum);
			switch(codenum)
			{
				case 1:
					userinsert(h,recvbuf);
                    printf("user insert success\n");
					break;
				case 2:
                    printf("wait loginfun\n");
					loginnum=login(h,recvbuf);
                    printf("login num = %d !\n",loginnum);
					if(loginnum==1)
                    {
                        sendbuf.code=0;
                        strcpy(sendbuf.username,recvbuf.username);
                        send(acceptfd,&sendbuf,sizeof(sendbuf),0);
                    }
					else
					{
                        sendbuf.code=1;
                        strcpy(sendbuf.username,recvbuf.username);
                        send(acceptfd,&sendbuf,sizeof(sendbuf),0);
                    }
					break;
				case 3:
					headinsert(H,recvbuf);
					break;
				case 4:
					viewuser(h,acceptfd);
                    usermessage(H,acceptfd);
					break;
				case 5:
                    viewallmessage(H,acceptfd);
				    break;
				
				default: 
				    break;
			}
		}
	}
}

link_node *creat()
{
	link_node *H=(link_node *)malloc(sizeof(link_node));
	memset(H->name,0,32);
	memset(H->time,0,128);
	memset(H->text,0,256);
	H->next=NULL;

	return H;
}

int headinsert(link_node *H,tcpsock recvdata)
{
	link_node *p=(link_node *)malloc(sizeof(link_node));
	strcpy(p->name,recvdata.name);
	strcpy(p->time,recvdata.time);
	strcpy(p->text,recvdata.text);
	p->next=H->next;
	H->next=p;

	return 0;
}

user_data *creatuserlink()
{
	user_data *H=(user_data *)malloc(sizeof(user_data));
	memset(H->username,0,32);
	memset(H->password,0,32);
	H->next=NULL;

	return H;
}

int userinsert(user_data *H,tcpsock recvdata)
{
	user_data *p=(user_data *)malloc(sizeof(user_data));
	strcpy(p->username,recvdata.username);
	strcpy(p->password,recvdata.password);
	p->next=H->next;
	H->next=p;

	return 0;
}

int login(user_data *H,tcpsock recvdata)
{
	user_data *p=H;
	while(p->next!=NULL)
	{
		p=p->next;
		if(strcmp(p->username,recvdata.username)==0)
		{
			if(strcmp(p->password,recvdata.password)==0)
			{
				return 1;
			}
			else
			{
				return 0;
			}	
		}
	}

	return 0;
}

int viewuser(user_data *H,int acceptfd)
{
    user_data *p=H;
    char sendname[34]={0};
    while(p->next!=NULL)
    {
        p=p->next;
        sprintf(sendname,"0:%s",p->username);
        send(acceptfd,sendname,34,0);
    }
    sprintf(sendname,"1:empty");
    send(acceptfd,sendname,34,0);

    return 0;
}

int usermessage(link_node *H,int acceptfd)
{   
    char recvusername[32]={0};
    recv(acceptfd,recvusername,32,0);
    link_node *p=H;
    user_mes sendusermes;
    while(p->next!=NULL)
    {
        p=p->next;
        if(0==strcmp(p->name,recvusername))
        {
            sendusermes.code=1;
            strcpy(sendusermes.name,p->name);
            strcpy(sendusermes.text,p->text);
            strcpy(sendusermes.time,p->time);
            send(acceptfd,&sendusermes,sizeof(sendusermes),0);
            memset(sendusermes.name,0,32);
            memset(sendusermes.time,0,128);
            memset(sendusermes.text,0,256);
        }
    }
    sendusermes.code=0;
    send(acceptfd,&sendusermes,sizeof(sendusermes),0);

    return 0;
}


int viewallmessage(link_node *H,int acceptfd)
{
    link_node *p=H;
    user_mes sendusermes;
    user_mes recvusermes;
    if(p->next!=NULL)
    {
        p=p->next;
        sendusermes.code=-3;
        send(acceptfd,&sendusermes,sizeof(sendusermes),0);
    }
    else
    {
        sendusermes.code=-2;
        send(acceptfd,&sendusermes,sizeof(sendusermes),0);
        return 0;
    }
    int i=0;

    while(recv(acceptfd,&recvusermes,sizeof(recvusermes),0)!=0)
    {
        if(recvusermes.code==1)
        {
            for(i=0;i<10;i++)
            {
                strcpy(sendusermes.name,p->name);
                strcpy(sendusermes.text,p->text);
                strcpy(sendusermes.time,p->time);
                if(p->next!=NULL)
                {
                    sendusermes.code=1;
                    send(acceptfd,&sendusermes,sizeof(sendusermes),0);
                    memset(sendusermes.name,0,32);
                    memset(sendusermes.time,0,128);
                    memset(sendusermes.text,0,256);
                    p=p->next;
                }
                else
                {
                    sendusermes.code=2;
                    send(acceptfd,&sendusermes,sizeof(sendusermes),0);
                    memset(sendusermes.name,0,32);
                    memset(sendusermes.time,0,128);
                    memset(sendusermes.text,0,256);
                    return 0;
                }


            }
        }
        else if(recvusermes.code==0)
        {
            return 0;
        }
    }
}
